package uit.jee.lib.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.jee.lib.bo.AuthorBO;
import uit.jee.lib.bo.BookBO;
import uit.jee.lib.dto.AuthorDTO;
import uit.jee.lib.dto.BookDTO;

/**
 *
 * @author ngloc_it
 */
@WebServlet(name="AuthorDetail", urlPatterns={"/AuthorDetail"})
public class AuthorDetail extends HttpServlet {
   
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    }
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();

        String function = req.getParameter("function");
        String authorId = req.getParameter("id");
        String authorName = req.getParameter("name");

        if (function.equalsIgnoreCase(function)) {
            try {
                LoadData(session, authorId);
            } catch (Exception ex) {
                Logger.getLogger(AuthorDetail.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (function.equalsIgnoreCase("reloadbook")) {
            ReloadBookOfAuther(resp);
            return;
        }        
        String path = "./jsps/AuthorDetail.jsp";
        resp.sendRedirect(path);
    }

    public void ReloadBookOfAuther(HttpServletResponse resp) {

//
//
//
//
    }

    private void LoadData(HttpSession session, String authorId) throws Exception {
        AuthorBO authorBo = new AuthorBO();
        BookBO bookBo = new BookBO();

        AuthorDTO authorDto = authorBo.GetAuthorById(authorId);
        if(authorDto != null){
            session.setAttribute("author", authorDto);
            ArrayList<BookDTO> books = bookBo.getBookByAuthorId(authorId);
            if ((books != null)
                    && (!books.isEmpty())) {
                session.setAttribute("books", books);
            }
        }
    }
}
